package com.huangsiyuan.myepidemic.service.impl;

import com.huangsiyuan.myepidemic.beans.DailyEpidemicInfo;
import com.huangsiyuan.myepidemic.beans.EpidemicDataInfo;
import com.huangsiyuan.myepidemic.beans.EpidemicInfo;
import com.huangsiyuan.myepidemic.beans.ProvinceInfo;
import com.huangsiyuan.myepidemic.mapper.EpidemicMapper;
import com.huangsiyuan.myepidemic.mapper.ProvinceMapper;
import com.huangsiyuan.myepidemic.service.EpidemicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EpidemicServiceImpl implements EpidemicService {
    @Autowired
    private EpidemicMapper epidemicMapper;
    @Autowired
    private ProvinceMapper provinceMapper;
    @Override
    public List<ProvinceInfo> saveData(DailyEpidemicInfo dailyEpidemicInfo,Integer userId) {
        Date current=new Date();
        //数据日期
        String[] ymd=dailyEpidemicInfo.getDate().split("-");
        short year=0,month=0,day=0;
        year=Short.parseShort(ymd[0]);
        month=Short.parseShort(ymd[1]);
        day=Short.parseShort(ymd[2]);
        for(EpidemicInfo epidemicInfo:dailyEpidemicInfo.getArray()){
            //设置录入数据的用户ID
            epidemicInfo.setUserId(userId);
            //设置数据的录入日期
            epidemicInfo.setInputDate(current);
            //设置数据对应的日期
            epidemicInfo.setDataYear(year);
            epidemicInfo.setDataMonth(month);
            epidemicInfo.setDataDay(day);
            this.epidemicMapper.saveInfo(epidemicInfo);
        }
        return this.provinceMapper.findNoDataProvince(year,month,day);
    }

    @Override
    public List<EpidemicDataInfo> findLastestData() {
        Calendar calendar=new GregorianCalendar();
        short year=0,month=0,day=0;
        year=(short) calendar.get(Calendar.YEAR);
        month=(short) (calendar.get(Calendar.MONTH)+1);
        day=(short) calendar.get(Calendar.DATE);
        Map<String,Short> condition=new HashMap<>();
        condition.put("year",year);
        condition.put("month",month);
        condition.put("day",day);
        //查询每个省份的累计数量和当日新增的数量
        List<EpidemicDataInfo> list=this.epidemicMapper.findLastestData(condition);
        return list;
    }
}
